
import {
  getUserList ,createUsers,deleteReq,detailsSearch,editdUser
} from '@/services/userManageSer';
import { Modal,message } from 'antd';
import {checkList}from '@/utils/utils'
export default {
  namepace: 'userManage',
  state: {
    list: [],
    pagination: {},
    updateModalVisibel: false,
    importModalVisibel :false,
    detailModalVisibel :false
  },
  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(getUserList, payload);
      checkList(response.list);
      yield put({
        type: 'save',
        payload: {
          user_list: response && response.return_code === 'SUCCESS' ? response.list : [],    //列表
          pagination: {
            current: response ? response.page_num : 1,
            pageSize: response  ? response.page_size : 10,
            total: response  ? response.total_elements : 0,
          },
        },
      });
    },

    *findUserList({ payload }, { call, put }) {
      const response = yield call(getUserList, payload);
      //checkList(response.list);
      yield put({
        type: 'save',
        payload: {
          user_list: response && response.return_code === 'SUCCESS' ? response.list : [],    //列表
          pagination: {
            current: response ? response.page_num : 1,
            pageSize: response  ? response.page_size : 10,
            total: response  ? response.total_elements : 0,
          },
        },
      });
    },





    *showUpdateModal({ payload }, { call, put }) {
      //const response = yield call(rolesFindAll, {});
      let updateData = undefined;
      //let updateModal_dept_list =undefined;
      if(payload.modal_type === 'update'){
        const userDataResponse =  yield call(detailsSearch, {user_id : payload.user_id});
        updateData = userDataResponse && userDataResponse.return_code == 'SUCCESS'  ? userDataResponse.data :undefined
      }

      yield put({
        type: 'save',
        payload: {
          modal_type :payload.modal_type,
          updateData:updateData ,
          updateModalVisibel:true,

        },
      });
    },
    *showDetail({ payload, callback }, { call, put }) {
      const response = yield call(detailsSearch, payload);
      if (response && response.return_code === 'SUCCESS') {
        yield put({
          type: 'save',
          payload: {
            detailData:response.data ,
            detailModalVisibel:true,
          },
        });
      }
    },
    *createUser({ payload, }, { call, put }) {
      const response = yield call(createUsers, payload);
      if (response && response.return_code === 'SUCCESS') {
        message.success('新增成功');
        yield put({
          type: 'save',
          payload: {
            updateModalVisibel: false,
          },
        });
        return true;
      }

    },
    *updateUser({ payload, }, { call, put }) {
      const response = yield call(editdUser, payload);
      if (response && response.return_code === 'SUCCESS') {
        message.success('修改成功');
        yield put({
          type: 'save',
          payload: {
            updateModalVisibel: false,
          },
        });
        return true;
      }
    },
    *deleteUser({ payload, }, { call, put }) {
      const response = yield call(deleteReq, payload);
      if (response && response.return_code === 'SUCCESS') {
        message.success('操作成功');
        return true;
      }
    }

  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
